package com.vgemv.jsutilitysdk.window;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;


/**
 * 作用:
 * 1,提供一个全屏的,悬浮的textView.
 */

public class JsWindowManager {

    public static boolean mb_canFloatViewMove = false;
    public static boolean mb_canFloatViewTouchThrough = true;

    public static WindowManager getActivityWindow(Activity activity) {
        WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        return windowManager;
    }


    public static void addViewToWindow(final View view,Context context) {
        final WindowManager wm = (WindowManager) context.getSystemService(
                Context.WINDOW_SERVICE);
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();

        // 设置window type,系统大于6.0,TYPE_APPLICATION_OVERLAY,否则是TYPE_SYSTEM_ALERT
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            //6.0
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        }else {
            params.type =  WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        /*
         * 如果设置为params.type = WindowManager.LayoutParams.TYPE_PHONE; 那么优先级会降低一些,
         * 即拉下通知栏不可见
         */
        // 设置图片格式，效果为背景透明
        params.format = PixelFormat.RGBA_8888;

        // 设置Window flag
        if(mb_canFloatViewTouchThrough) {
            //下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸，不接受任何事件,同时不影响后面的事件响应。
            params.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        }
        else {
            params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        }
        params.gravity = android.view.Gravity.TOP;

        // 设置悬浮窗的长得宽
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.x = 0;
        params.y = 0;

        // 设置悬浮窗的Touch监听
        if(mb_canFloatViewMove) {
            view.setOnTouchListener(new View.OnTouchListener() {
                int lastX, lastY;
                int paramX, paramY;

                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            lastX = (int) event.getRawX();
                            lastY = (int) event.getRawY();
                            paramX = params.x;
                            paramY = params.y;
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int dx = (int) event.getRawX() - lastX;
                            int dy = (int) event.getRawY() - lastY;
                            params.x = paramX + dx;
                            params.y = paramY + dy;
                            // 更新悬浮窗位置
                            wm.updateViewLayout(view, params);
                            break;
                    }
                    return true;
                }
            });
        }
        else {

        }
        try {
            //需要有权限才能增加
            wm.addView(view, params);
        }
        catch(Exception e) {
            e.printStackTrace();
            throw new AssertionError("GajWindowManager:No overlay permission");
        }

    }
}
